﻿using System.CodeDom;
using System.Linq;
using Roslyn.Compilers.CSharp;

namespace InterfaceWeaver.CustomTool
{
    public sealed class NamespaceDeclaration
    {
        private readonly NamespaceDeclarationSyntax @namespace = null;

        /// <summary>
        /// Initializes a new instance of the <see cref="InterfaceDeclaration" /> class.
        /// </summary>
        /// <param name="namespace">The namespace.</param>
        public NamespaceDeclaration(NamespaceDeclarationSyntax @namespace)
        {
            this.@namespace = @namespace;
        }


        /// <summary>
        /// Creates the code namespace.
        /// </summary>
        /// <returns></returns>
        public CodeNamespace CreateCodeNamespace(CompilationUnitSyntax parsedCodeRoot)
        {
            var weavedNamespace = new CodeNamespace
                {
                Name = @namespace.Name.ToString()
            };

            // Import interface namespaces
            weavedNamespace.Imports.AddRange(
                @namespace.Usings
                .Select(p => new CodeNamespaceImport(p.Name.ToFullString().Trim())).ToArray());
            weavedNamespace.Imports.AddRange(parsedCodeRoot.Usings.Select(p => new CodeNamespaceImport(p.Name.ToFullString().Trim())).ToArray());

            return weavedNamespace;
        }
    }
}
